package com.pomelo.kill.controller;

import com.pomelo.kill.common.AjaxResult;
import com.pomelo.kill.model.Userinfo;
import com.pomelo.kill.service.UserinfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserinfoController {

    @Resource
    private UserinfoService userInfoService;


    /**
     * 用户登录
     *
     * @param openid 小程序提供的openid
     */
    @RequestMapping("/login")
    public AjaxResult login(String openid) {
        Userinfo userinfo = userInfoService.selectByOpenid(openid);
        if (userinfo != null) {
            return AjaxResult.success(userinfo);
        }
        Userinfo login = userInfoService.login(openid);
        return login == null ? AjaxResult.failure("登录失败") : AjaxResult.success(login);
    }

    /**
     * 修改用户信息
     *
     * @param userinfo 修改的用户实体
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Userinfo userinfo) {
        int i = userInfoService.updateByPrimaryKeySelective(userinfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.failure();
    }


}
